<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables, $allow_navcomp;
global $debug;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

BigTitle($txt['bnt_nav_title']);

//-------------------------------------------------------------------------------------------------

if (!$allow_navcomp)
{
	echo $txt['bnt_nav_nocomp'], '<br><br>';
	TextMainMenu();
	return;
}

// Pull player info into $playerinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$current_sector = $playerinfo['sector'];
$computer_tech  = $playerinfo['computer'];

// Pull sector info into $sectorinfo.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $current_sector
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

$state = (isset($_POST['state'])) ? $_POST['state'] : '';
$stop_sector = (isset($_POST['stop_sector'])) ? $_POST['stop_sector'] : '';

if ($state == 0)
{
	echo '
		<form action="', $scripturl, '?action=blacknova;sa=navcomp" method="post">
		', $txt['bnt_nav_query'], ' <input name="stop_sector">&nbsp;<input type="submit" value="', $txt['bnt_submit'], '"><br>
		<input name="state" value="1" type="hidden">
		</form><br><br>';
}
elseif ($state == 1)
{
	if ($computer_tech < 5)
		$max_search_depth = 2;
	elseif ($computer_tech < 10)
		$max_search_depth = 3;
	elseif ($computer_tech < 15)
		$max_search_depth = 4;
	elseif ($computer_tech < 20)
		$max_search_depth = 5;
	else
		$max_search_depth = 6;
		
	for ($search_depth = 1; $search_depth <= $max_search_depth; $search_depth++)
	{
		$search_query = 'SELECT	distinct a1.link_start, a1.link_dest ';
		
		for ($i = 2; $i <= $search_depth; $i++)
		{
			$search_query .= ' ,a' . $i . '.link_dest ';
		}
		
		$search_query .= 'FROM ' . $dbtables['links'] . ' AS a1 ';

		for ($i = 2; $i <= $search_depth; $i++)
		{
			$search_query .= ' ,' . $dbtables['links'] . ' AS a' . $i . ' ';
		}
		
		$search_query .= 'WHERE a1.link_start = ' . $current_sector . ' ';

		for ($i = 2; $i <= $search_depth; $i++)
		{
			$k = $i - 1;
			$search_query .= ' AND a' . $k . '.link_dest = a' . $i . '.link_start ';
		}
			
		$search_query .= ' AND a' . $search_depth . '.link_dest = ' . $stop_sector . ' ';
		$search_query .= ' AND a1.link_dest != a1.link_start ';
			
		for ($i = 2; $i <= $search_depth; $i++)
		{
			$search_query .= ' AND a' . $i . '.link_dest NOT IN (a1.link_dest, a1.link_start ';

			for ($j = 2; $j < $i; $j++)
			{
				$search_query .= ',a' . $j . '.link_dest ';
			}
			$search_query .= ')';
		}
			
		$search_query .= 'ORDER BY a1.link_start, a1.link_dest ';
			
		for ($i = 2; $i <= $search_depth; $i++)
		{
			$search_query .= ', a' . $i . '.link_dest';
		}
			
		$search_query .= ' LIMIT 1';
		
		if ($debug)
			echo '<br>', $search_query, '<br>';

		$result = $smcFunc['db_query']('', 
			$search_query,
			''
		);
		
		$found = $smcFunc['db_num_rows']($result);
		
		if ($found > 0)
			break;
	}
	
	if ($found > 0)
	{
		echo '<h3>', $txt['bnt_nav_pathfnd'], '</h3>';
		$links = $smcFunc['db_fetch_row']($result);
		$smcFunc['db_free_result']($result);
		echo $links[0];
		for ($i = 1; $i < $search_depth + 1; $i++)
		{
			echo ' >> ' . $links[$i];
		}
		echo '<br><br>';
		echo $txt['bnt_nav_answ1'], ' ', $search_depth, ' ', $txt['bnt_nav_answ2'], '<br><br>';
	}
	else
		echo $txt['bnt_nav_proper'], '<br><br>';

}

TextMainMenu();
?>
